<?php

/**
 * This is the model class for table "{{tokens}}".
 *
 * The followings are the available columns in table '{{tokens}}':
 * @property integer $id_user
 * @property string $life_time
 * @property string $token
 */
class Tokens extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Tokens the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{tokens}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id_user, life_time, token', 'required'),
			array('id_user', 'numerical', 'integerOnly'=>true),
			array('token', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id_user, life_time, token', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_user' => 'Id User',
			'life_time' => 'Life Time',
			'token' => 'Token',
		);
	}
	
	public function generateToken()
	{
            return md5(rand());
	}
	
	public function saveToken()
	{
	}
        
        public function deleteUserTokens($id_user)
        {
            //$this->deleteAll(array('id_user' => $id_user));
            $connection=Yii::app()->db; // так можно делать, если в конфигурации настроен компонент соединения "db"
            // В противном случае можно создать соединение явно:
            // $connection=new CDbConnection($dsn,$username,$password);
            $sql = 'DELETE FROM '.$this->tableName().' WHERE id_user='.$id_user;
            $command = $connection->createCommand($sql);
            $command->execute();
        }
        
        public function getUserByToken($token)
        {
            $connection=Yii::app()->db;
            $sql = 'SELECT id_user FROM '.$this->tableName().' WHERE token="'.$token.'"';
            $command = $connection->createCommand($sql);
            $dataReader = $command->query();
            $id_user = 0;
            foreach($dataReader as $row) {
                if ($row['id_user'] > 0){
                    $id_user = $row['id_user'];
                    break;
                }
            }
            return $id_user;
        }

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id_user',$this->id_user);
		$criteria->compare('life_time',$this->life_time,true);
		$criteria->compare('token',$this->token,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}